package com.shop.seckill.service.impl;

import com.base.common.entity.SeckillGoods;
import com.base.common.entity.SeckillOrder;
import com.base.common.http.HttpResult;
import com.base.common.utils.SeckillStatus;
import com.shop.seckill.feign.ApiWeixinPayFeign;
import com.shop.seckill.service.SecKillOrderService;
import com.shop.seckill.task.MultiThreadingCreateOrder;
import com.shop.seckill.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class SeckillOrderServiceImpl extends BaseServiceImpl<SeckillOrder> implements SecKillOrderService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private MultiThreadingCreateOrder multiThreadingCreateOrder;

    @Autowired
    private ApiWeixinPayFeign apiWeixinPayFeign;

    /***
     * 获取秒杀状态
     * @param userId
     * @return
     */
    @Override
    public SeckillStatus getStatusByUserId(String userId) {
        SeckillStatus seckillStatus =  (SeckillStatus) redisUtils.hget("seckillGoods_Order_Queue_Tag", userId);
        //秒杀状态  1:排队中，2:秒杀等待支付,3:支付超时，4:秒杀失败,5:支付完成
        //如果状态为秒杀失败，需要清理秒杀排队标记
        if(seckillStatus!=null && seckillStatus.getStatus().intValue()==4){

            redisUtils.hdel("seckillGoods_Order_Queue_Tag",userId);
            //redisTemplate.boundHashOps("seckillGoods_Order_Queue_Tag").delete(username);
        }
        return seckillStatus;
    }

    @Transactional
    @Override
    public HttpResult addSeckillOrder(String userId, Long seckillId) {
        //获取用户抢单的信息
        Object order = redisUtils.hget("seckillOrder", userId);
        if(order!=null){
            return HttpResult.error("存在未支付秒杀订单");
//            throw new RuntimeException("存在未支付订单！");
        }
        //排队标示
        SeckillStatus checkSeckillStatus = (SeckillStatus) redisUtils.hget("seckillGoods_Order_Queue_Tag", userId);
        if(checkSeckillStatus!=null && checkSeckillStatus.getStatus()==1){
            return HttpResult.error("您已经在抢购商品！");
//            throw new RuntimeException("您已经在抢购商品！");
        }
        //查询商品信息
        SeckillGoods seckillGoods = (SeckillGoods) redisUtils.hget("seckillGoods",String.valueOf(seckillId));
        //判断当前商品是否有库存
        if(seckillGoods==null || seckillGoods.getStockCount()<=0){
//            throw new RuntimeException("已售罄");
            return HttpResult.error("已售罄");
        }
        //创建秒杀队列数据，秒杀排队
        SeckillStatus seckillStatus = new SeckillStatus(userId, new Date(), 1, seckillId);
        //将秒杀数据存入到队列
        redisUtils.lpush("seckillGoods_Order_Queue_Up",seckillStatus);
        //存一个抢购标示,方便做状态查询
        redisUtils.hset("seckillGoods_Order_Queue_Tag", userId,seckillStatus);
        //开启异步方法调用,多线程下单
        multiThreadingCreateOrder.createOrder();
        return HttpResult.ok("添加秒杀订单成功");

    }

    /**
     * 获取待支付的订单信息
     * @param userId
     * @return
     */
    @Override
    public SeckillOrder getOrderByUserId(String userId) {

        //获取支付订单
        SeckillOrder seckillOrder = (SeckillOrder) redisUtils.hget("seckillOrder", userId);
        return seckillOrder;
    }

    @Override
    public SeckillGoods findSecKillOrder(String userId) {
        SeckillOrder order = this.getOrderByUserId(userId);

        /** 秒杀抢购的商品 */
        SeckillGoods goods = (SeckillGoods) redisUtils.hget("seckillGoods",String.valueOf(order.getSeckillId()));
        return  goods;
    }

    @Override
    public HttpResult createNativeQrcode(Long userId) {

        //获取订单信息
        SeckillOrder seckillOrder = this.getOrderByUserId(String.valueOf(userId));

        BigDecimal money = seckillOrder.getMoney();
        // 微信支付，没有小数位，用个位数代表分位，用十位数代表角，用百位数代表元，因此要把数据库的金额乘以100
        Integer moneyInt = money.multiply(new BigDecimal("100")).intValue();

        return apiWeixinPayFeign.createCode(String.valueOf(seckillOrder.getId()),String.valueOf(moneyInt));
    }

    @Override
    public void updateStatusSeckillOrder(String userId, String transactionId, String payTime) {

    }

    @Override
    public void deleteOrder(String userId) {



    }
}
